package bagu.juc.lock;

import java.util.concurrent.locks.ReentrantLock;

public class ReentantlockDemo01 {
    private final ReentrantLock lock = new ReentrantLock();

    private void criticalSection() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + " is running");
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        ReentantlockDemo01 reentantlockDemo01 = new ReentantlockDemo01();

        Runnable task = reentantlockDemo01::criticalSection;



        Thread thread1 = new Thread(task, "线程1");
        Thread thread2 = new Thread(task, "线程2");

        thread1.start();
        thread2.start();

    }
}
